package com.mmall.concurrency.example.singleton;

import com.mmall.concurrency.annoations.ThreadSafe;

/**
 * 饿汉模式
 * 单例实例在类装载使用时创建
 */
@ThreadSafe
public class SingletonExample6 {

    //私有构造函数
    private SingletonExample6(){
        //私有构造函数不复杂
        //这个类一定会使用
    }
    //单例对象
    private static SingletonExample6 instance = null;

    static{
        instance = new SingletonExample6();  //静态变量和静态代码块谁先谁早执行
    }

    //静态的工厂方法
    public static SingletonExample6 getInstance(){
        return instance;
    }

    public static void main(String args[]){
        System.out.println(getInstance().hashCode());
        System.out.println(getInstance().hashCode());
    }
}
